package com.turing.post.tuxedo.service;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.transaction.util.MrchBusiness;
import com.turing.post.transaction.util.MrchTransParam;
import com.turing.post.tuxedo.util.IDepositValidate;
import com.turing.post.tuxedo.util.LocalService;
import com.turing.post.tuxedo.util.LocalServiceImpl;
import com.turing.post.tuxedo.util.ODepositValidate;
import com.turing.post.tuxedo.util.PCPSService;
import com.turing.post.tuxedo.util.TransRequest;
import com.turing.post.tuxedo.util.TransResponse;
import com.turing.post.tuxedo.util.TuxedoRespCode;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "PCPS.SVC9044")
@Remote( { LocalService.class, PCPSService.class })
@Local
public class DepositValidate extends LocalServiceImpl implements LocalService,
		PCPSService {

	@EJB(mappedName = "transaction.impl.MrchBusinessImpl")
	private MrchBusiness mrchBusinessImpl;

	/**
	 * generate IDepositValidate
	 * 
	 * @return IDepositValidate
	 */
	public IDepositValidate makeRequestObject() {
		return new IDepositValidate();
	}

	/**
	 * DepositValidate 数据检查
	 * 
	 * @param req
	 * @param ack
	 */
	protected void preService(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {
	}

	/**
	 * DepositValidate service entry 预付卡充值验证的业务逻辑
	 * 
	 * @param req
	 * @param ack
	 */
	protected void Service(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {

		IDepositValidate itr = (IDepositValidate) req;
		ODepositValidate otr = (ODepositValidate) ack;

		// 设置基本参数
		MrchTransParam imtp = setMrchTransParam(req);

		// 验证卡信息的完整性
		itr.cardObj.cardNo = mrchBusinessImpl.getValidateCardNo(req.merchantNo,
				itr.cardObj.cardNo, itr.cardObj.track2, itr.cardObj.track3);

		// 充值卡验证对象
		imtp.depositCard = imtp.new CardAccount();
		imtp.depositCard.cardNo = itr.cardObj.cardNo;
		imtp.depositCard.track2 = itr.cardObj.track2;
		imtp.depositCard.track3 = itr.cardObj.track3;

		// 代理操作员号
		imtp.OperId = null;

		// 执行交易
		MrchTransParam omtp = mrchBusinessImpl.DepositValidate(imtp);

		// 主机流水号
		otr.tranTime = itr.curSysDateTime;
		otr.respCode = TuxedoRespCode.RC_SUCCESS;

		// 返回的卡号及余额信息
		otr.depositLimit = omtp.depositCard.depositLimit;
	}

	/**
	 * DepositValidate 服务入口
	 * 
	 * @param req
	 * @return TransResponse
	 */
	public TransResponse Service(TransRequest req)
			throws PCPSApplicationException {
		// 按普通方法执行服务
		return CommonService(req, new ODepositValidate());
	}
}